package main

import (
	"encoding/json"
	"fmt"
	"github.com/nacos-group/nacos-sdk-go/clients"
	"github.com/nacos-group/nacos-sdk-go/common/constant"
	"github.com/nacos-group/nacos-sdk-go/vo"
	"shop/nacos_test/config"
)

func main() {
	//server config
	serverConfigs := []constant.ServerConfig{
		{
			IpAddr:      "localhost",
			ContextPath: "/nacos",
			Port:        8848,
			Scheme:      "http",
		},
	}

	//client config
	clientConfig := constant.ClientConfig{
		NamespaceId:         "da9de03f-2487-4ee4-8d12-9584a19eb631", // 如果需要支持多namespace，我们可以创建多个client,它们有不同的NamespaceId。当namespace是public时，此处填空字符串。
		TimeoutMs:           5000,
		NotLoadCacheAtStart: true,
		LogDir:              "tmp/nacos/log",
		CacheDir:            "tmp/nacos/cache",
		LogLevel:            "debug",
	}

	// 创建动态配置客户端
	client, err := clients.CreateConfigClient(map[string]interface{}{
		"serverConfigs": serverConfigs,
		"clientConfig":  clientConfig,
	})
	if err != nil {
		panic(err)
	}
	content, err := client.GetConfig(vo.ConfigParam{
		DataId: "usr-srcv.json",
		Group:  "dev"})
	if err != nil {
		panic(err)
	}

	fmt.Println(content)

	servConfing := config.ServerConfig{}

	err = json.Unmarshal([]byte(content), &servConfing)
	if err != nil {
		panic(err)
	}
	fmt.Println(servConfing)

	fmt.Println(content)

	//fmt.Println("########")
	//err = client.ListenConfig(vo.ConfigParam{
	//	DataId: "usr-web.yaml",
	//	Group:  "dev",
	//	OnChange: func(namespace, group, dataId, data string) {
	//		fmt.Println("配置文件发生了变化")
	//		fmt.Println("group:" + group + ", dataId:" + dataId + ", data:" + data)
	//	},
	//})
	//if err != nil {
	//	panic(err)
	//}
	//
	//time.Sleep(3000 * time.Second)
}
